package com.sparkStreaming.demo

import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}

/**
  * Spark Streaming处理Socket数据
  * sudo yum install -y nc
  * 测试： nc -lk 6789
  */
object _11_NetworkWordCount {


  def main(args: Array[String]): Unit = {

    val sparkConf =
      new SparkConf()
        .setMaster("local[6]")
        .setAppName("NetworkWordCount")

    /**
      * 创建StreamingContext需要两个参数：SparkConf和batch interval
      */
    val ssc = new StreamingContext(sparkConf, Seconds(5))

    //nc -lk 6789
    val dStream1 = ssc.socketTextStream("hadoop000", 6789)

    val dStream2 = dStream1
      .flatMap(_.split(" "))//【a,b,c,a】
      .map((_, 1))//(a,1)(b,1)(c,1)(a,1)
      .reduceByKey(_ + _)

    dStream2.print()

    ssc.start()
    ssc.awaitTermination()
  }
}
